//
//  ANProductViewController.m
//  Ants
//
//  Created by ZHM on 16/3/28.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANProductViewController.h"

#import "ANProfieleHeader.h"
#import "ANTableViewProfileCell.h"
#import "ANProductImageScrollViewCell.h"
#import "ANProductDetailTableViewCell.h"
#import "ANProductDetailCellFrame.h"
#import "ANProductInfoViewController.h"
#import "ANBuyCollectionView.h"
#import "ANShoppingCarViewController.h"
#import "ANUserLoginViewController.h"
#import "ANNavigationVC.h"

#import "MJPhotoBrowser.h"
#import "MJPhoto.h"

@interface ANProductViewController ()<MJPhotoBrowserDelegate,ANBuyCollectionViewDelegate>

@property (nonatomic, strong) ANProductDetailCellFrame *productDetailCellFrame;

@property (nonatomic, weak) ANProductImageScrollViewCell *scrollViewCell;
@end

@implementation ANProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"商品详情";
    //添加第1组
    [self setUpGroup_1];
    
    //查找
    [self findProduct];
}

- (void)loadView {
    [super loadView];
    [[SDImageCache sharedImageCache] setShouldDecompressImages:NO];
    [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];
    
    //删除购物车 与另一个商品详情
//    NSMutableArray *VCs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
//    for (int i = 0;i < VCs.count;i++) {
//        UIViewController *vc = VCs[i];
//        if ([vc isKindOfClass:[ANShoppingCarViewController class]]) {
//            ANShoppingCarViewController *oneVc = (ANShoppingCarViewController *)vc;
//            if (!oneVc.isBootCV) {
//                [VCs removeObject:oneVc];
//            }
//        }else if ([vc isKindOfClass:[ANProductViewController class]] && !(i == VCs.count -1)) {
//            [VCs removeObject:vc];
//        }
//    }
//    self.navigationController.viewControllers = VCs;
}

- (void)findProduct {
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"Product"];
    [bquery getObjectInBackgroundWithId:_product.mlObjectId block:^(BmobObject *object,NSError *error){
       if (error.code == 101){
            [self showAlertWithTitle:@"商品已经到火星啦!!"];
        }
    }];
}
- (void)showAlertWithTitle:(NSString *)title {
    if (iOS8Later) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
        // 创建操作
        [alertController addAction:[UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [sharedWebService removeProduct:_product isRemoveWeb:NO completion:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
    } else {
        //还没完成设置ioS8 以前系统
        [[[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil] show];
    }
}
- (void)showProduct:(ANBDProduct *)product {
    self.product = product;
    [self.tableView reloadData];
}
- (void)buyCollectionView:(ANBuyCollectionView *)buyCollectionView didClickBtn:(NSInteger)index {

    if (![sharedWebService isLoggedIn]) {
        ANUserLoginViewController *userLoginVC = [[ANUserLoginViewController alloc] init];
        userLoginVC.loginComplete = ^{
            switch (index) {
                case 0:
                    [self favoriteClick];
                    break;
                case 1:
                    [self shoppingCarClick];
                    break;
                case 2:
                    [self addShopingCarClick];
                    break;
            }
        };
        ANNavigationVC *nav = [[ANNavigationVC alloc] initWithRootViewController:userLoginVC];
        [self presentViewController:nav animated:YES completion:nil];
    }else {
        switch (index) {
            case 0:
                [self favoriteClick];
                break;
            case 1:
                [self shoppingCarClick];
                break;
            case 2:
                [self addShopingCarClick];
                break;
        }
    }
}
- (void)favoriteClick {
    
}
- (void)shoppingCarClick {
    //
    
    if (_isFromShoppingCar) {
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        ANShoppingCarViewController *vc = [[ANShoppingCarViewController alloc] init];
        vc.title = @"购物车";
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)addShopingCarClick {
    
    [sharedWebService addShoppingItemWithProduct:_product Completion:^(BOOL succeeded, NSError *error) {
        if (!error) {
            NSDictionary *dict = @{@"PRODUCT":@"aaaaaaadfsdfefe"};
            NSNotification *notification = [NSNotification notificationWithName:@"RefreshShoppingCar" object:nil userInfo:dict];
            [[NSNotificationCenter defaultCenter] postNotification:notification];
            //发送通知 刷新
            //[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshShoppingCar" object:nil];
            [self shoppingCarClick];
        } else {
            if (error.code == 206) {
                [sharedWebService logout];
                ANUserLoginViewController *userLoginVC = [[ANUserLoginViewController alloc] init];
                ANNavigationVC *nav = [[ANNavigationVC alloc] initWithRootViewController:userLoginVC];
                [self presentViewController:nav animated:YES completion:nil];
            } else {
                [self shoppingCarClick];
            }
        }
    }];
}

- (void)setUpGroup_1 {
    //显示商品工具条
    NSArray *titles = @[@"收藏商品",@"购物车",@"购买"];
    [ANBuyCollectionView buyCollectionViewWithViewController:self titles:titles animation:NO];
    
    ANArrowItem *productInfo = [ANArrowItem itemWithTilte:@"产品评价"];
    ANArrowItem *productComment = [ANArrowItem itemWithTilte:@"产品规格"];
    productComment.destVcClass = [ANProductInfoViewController class];
    
    ANGuoupItem *group = [[ANGuoupItem alloc] init];
    //    group.headedTitle = @"aaa";
    //    group.footerTitle = @"bbb";
    group.items = @[productInfo,productComment];
    
    ANGuoupItem *groupEmpty = [[ANGuoupItem alloc] init];
    [self.guoups addObject:groupEmpty];
    [self.guoups addObject:groupEmpty];
    [self.guoups addObject:group];
}

- (void)setProduct:(ANBDProduct *)product {
    _product = product;
    ANProductDetailCellFrame *productDetailCellFrame = [[ANProductDetailCellFrame alloc] init];
    productDetailCellFrame.product = product;
    _productDetailCellFrame = productDetailCellFrame;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        ANProductImageScrollViewCell *cell = [ANProductImageScrollViewCell cellWithTableView:tableView];
        [cell configureCell:self.product];
        _scrollViewCell = cell;
        return cell;
    }
    if (indexPath.section == 1) {
        ANProductDetailTableViewCell *cell = [ANProductDetailTableViewCell cellWithTableView:tableView];
        cell.prodcutDetailCellFrame = _productDetailCellFrame;
        return cell;
    }

    ANTableViewProfileCell *cell = [ANTableViewProfileCell cellWithTableView:tableView];
    ANGuoupItem *group = self.guoups[indexPath.section];
    ANProfileBaseItem *item = group.items[indexPath.row];
    cell.item = item;
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.guoups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section <= 1) {
        return 1;
    }
    ANGuoupItem *guoupItem = self.guoups[section];
    return guoupItem.items.count;
}
#pragma mark -- 重写跳转方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 214;
    }
    if (indexPath.section == 1) {
        return _productDetailCellFrame.cellHeight;
    }
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section > 1) {
        return 10;
    }
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

#pragma mark -点击CELL调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.indexCellClick = indexPath;
    ANGuoupItem *group = self.guoups[indexPath.section];
    ANProfileBaseItem *item = group.items[indexPath.row];
    if (item.option) {
        item.option(item);
    }else if (item.destVcClass) {
        ANProductInfoViewController *vc = [[ANProductInfoViewController alloc] init];
        vc.product = self.product;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)dealloc {
    [[SDImageCache sharedImageCache] setShouldDecompressImages:YES];
    [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:YES];
    NSLog(@"%s",__func__);
}
@end
